package com.yuyang.wifi.activitys.mine;

import android.os.Handler;
import android.widget.TextView;

import com.yuyang.wifi.R;
import com.yuyang.wifi.base.BaseActivity;
import com.yuyang.wifi.base.BaseResult;
import com.yuyang.wifi.constant.AppConstant;
import com.yuyang.wifi.model.AdTypeInfoModel;
import com.yuyang.wifi.model.AdTypeInfoResult;
import com.yuyang.wifi.utils.ActivityAllManager;
import com.yuyang.wifi.utils.RxUtil;
import com.yuyang.wifi.utils.Skip;
import com.yuyang.wifi.utils.SpUtil;
import com.yuyang.wifi.utils.ToastUtils;
import com.yuyang.wifi.utils.network.BaseDataSubscriber;
import com.yuyang.wifi.utils.network.HttpErrorHandler;
import com.yuyang.wifi.utils.network.HttpManager;
import com.yuyang.wifi.utils.network.RxDataInstance;
import com.yuyang.wifi.views.LoadCSJiliVideoUtils;
import com.yuyang.wifi.views.LoadCsjInsertAdUtils;
import com.yuyang.wifi.views.LoadCsjNewInsertFullAdUtils;
import com.yuyang.wifi.views.LoadYLHJiliVideoUtils;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PhoneCoolGifActivity extends BaseActivity {

    @BindView(R.id.tv_cool_num)
    TextView tvCoolNum;
    private LoadCsjNewInsertFullAdUtils newInsertFullAd;
    private LoadYLHJiliVideoUtils loadYLHJiliVideoUtils;
    private int factorycode;

    @Override
    protected void initActionBar() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_phone_cool_gif;
    }

    @Override
    protected void initContentView() {
        newInsertFullAd = LoadCsjNewInsertFullAdUtils.getInstance();
        newInsertFullAd.load(this, "csj_jl_cool");
        newInsertFullAd.setLoadFailureListener(new LoadCsjNewInsertFullAdUtils.OnLoadFailureListener() {
            @Override
            public void onLoadFailureListener() {
                playErrorRepalceAd(2);
            }
        });

        loadYLHJiliVideoUtils = LoadYLHJiliVideoUtils.getInstance();
        loadYLHJiliVideoUtils.load(this, "ylh_jl_cool");
        loadYLHJiliVideoUtils.setLoadFailureListener(new LoadYLHJiliVideoUtils.OnLoadFailureListener() {
            @Override
            public void onLoadFailureListener() {
                playErrorRepalceAd(1);
            }
        });
        showChange();
        getAdTypeInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void showChange() {

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(55.8);
            }
        }, 300);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(52.6);
            }
        }, 600);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(47.1);
            }
        }, 900);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(43.8);
            }
        }, 1200);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(42.0);
            }
        }, 1500);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(40.6);
            }
        }, 1800);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(39.8);
            }
        }, 2100);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(38.2);
            }
        }, 240000);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(37.7);
            }
        }, 2700);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setData(34.5);
            }
        }, 3000);

    }

    public void setData(Double num) {
        if (null != tvCoolNum) {
            tvCoolNum.setText(num + "°c");
        }
    }

    private void getAdTypeInfo() {
        RxDataInstance.getInstance().initMap(PhoneCoolGifActivity.this)
                .put("entryNumber", "8");
        HttpManager.getInstance().getApi().getAdTypeInfo(RxDataInstance.getInstance().getParamMap())
                .compose(RxUtil.<AdTypeInfoResult>bindLifecycleAndApplySchedulers(this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseDataSubscriber<AdTypeInfoResult>(new HttpErrorHandler() {
                    @Override
                    public void onHttpError(BaseResult result) {
                        ToastUtils.showShortToast(PhoneCoolGifActivity.this, result.getErrorMessage());
                    }

                    @Override
                    public void onHttpException(Throwable e) {
                        // dismissLoadingDialog();
                    }
                }) {
                    @Override
                    public void onDataNext(AdTypeInfoResult result) {
                        int resultCode = result.getResultCode();
                        if (0 == resultCode) {
                            AdTypeInfoModel data = result.getData();
                            if (null != data) {
                                factorycode = data.getType();
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (factorycode == 1) {
                                            newInsertFullAd.showNewInterAd();
                                            newInsertFullAd.setItemAllClickListener(new LoadCsjNewInsertFullAdUtils.OnItemAllClickListener() {
                                                @Override
                                                public void onItemAllClickListener() {
                                                    jumpNext();
                                                }
                                            });
                                        } else if (factorycode == 2) {
                                            loadYLHJiliVideoUtils.play();
                                            loadYLHJiliVideoUtils.setAdCloseListener(new LoadYLHJiliVideoUtils.OnAdCloseListener() {
                                                @Override
                                                public void onAdCloseListenerListener(boolean isPlayComplete) {
                                                    jumpNext();
                                                }
                                            });
                                        }
                                    }
                                }, 3000);
                            }
                        } else {
                            ToastUtils.showShortToast(PhoneCoolGifActivity.this, result.getErrorMessage());
                        }
                    }
                });
    }

    private void playErrorRepalceAd(int type) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (type == 1) {
                    newInsertFullAd.showNewInterAd();
                    newInsertFullAd.setItemAllClickListener(new LoadCsjNewInsertFullAdUtils.OnItemAllClickListener() {
                        @Override
                        public void onItemAllClickListener() {
                            jumpNext();
                        }
                    });
                } else {
                    loadYLHJiliVideoUtils.play();
                    loadYLHJiliVideoUtils.setAdCloseListener(new LoadYLHJiliVideoUtils.OnAdCloseListener() {
                        @Override
                        public void onAdCloseListenerListener(boolean isPlayComplete) {
                            jumpNext();
                        }
                    });
                }
            }
        }, 1000);
    }

    private void jumpNext() {
        ActivityAllManager.getInstance().finishActivity(PhoneCoolGifActivity.class);
        Skip.toActivity(PhoneCoolGifActivity.this, PhoneCoolActivity.class);
    }

}
